package com.imooc.user.controller;

import com.imooc.user.constant.CookieConstant;
import com.imooc.user.constant.RedisConstant;
import com.imooc.user.dataobject.UserInfo;
import com.imooc.user.enums.ResultEnum;
import com.imooc.user.enums.RoleEnum;
import com.imooc.user.service.UserService;
import com.imooc.user.utils.CookieUtil;
import com.imooc.user.utils.ResultVOUtil;
import com.imooc.user.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * @author : 浦希成
 * @date : 2018/8/2 16:52
 */
@RestController
@RequestMapping("/login")
public class LoginController {
    @Autowired
    private UserService userService;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    /**
     * 买家登录
     *
     * @param openid   openid
     * @param response HttpServletResponse对象
     * @return ResultVO
     */
    @GetMapping("/buyer")
    public ResultVO buyer(@RequestParam String openid, HttpServletResponse response) {
        //1.openid 和数据库进行匹配
        UserInfo userInfo = userService.findByOpenId(openid);
        if (userInfo == null) {
            return ResultVOUtil.error(ResultEnum.LOGIN_FAIL);
        }
        //2.判断角色
        if (!RoleEnum.BUYER.getCode().equals(userInfo.getRole())) {
            return ResultVOUtil.error(ResultEnum.ROLE_ERROR);
        }
        //3.设置cookie
        CookieUtil.set(response, CookieConstant.OPENID, openid, CookieConstant.EXPIRE);
        return ResultVOUtil.success();

    }

    /**
     * 卖家登录
     *
     * @param openid   openid
     * @param response HttpServletResponse对象
     * @return ResultVO
     */
    @GetMapping("/seller")
    public ResultVO seller(@RequestParam String openid, HttpServletResponse response, HttpServletRequest request) {
        //判断是否已登录
        Cookie cookie=CookieUtil.get(request,CookieConstant.TOKEN);
        if (cookie!=null&&
                stringRedisTemplate.opsForValue().get(RedisConstant.TOKEN_TEMPLATE+"_"+cookie.getValue())!=null){
            ResultVOUtil.success();
        }



        //1.openid 和数据库进行匹配
        UserInfo userInfo = userService.findByOpenId(openid);
        if (userInfo == null) {
            return ResultVOUtil.error(ResultEnum.LOGIN_FAIL);
        }
        //2.判断角色
        if (!RoleEnum.SELLER.getCode().equals(userInfo.getRole())) {
            return ResultVOUtil.error(ResultEnum.ROLE_ERROR);
        }
        // 3.redis设置key=UUID，value=openid
        String token= UUID.randomUUID().toString();
        //过期时间和redis 保持一致
        Integer expire=CookieConstant.EXPIRE;
        stringRedisTemplate.opsForValue().set(RedisConstant.TOKEN_TEMPLATE+"_"+token,openid,expire, TimeUnit.SECONDS);
        //4.设置cookie
        CookieUtil.set(response, CookieConstant.TOKEN, token, CookieConstant.EXPIRE);
        return ResultVOUtil.success();
    }
}
